package beautiful.butterfly.schedule.test;

import beautiful.butterfly.schedule.SingleTaskDeal;
import beautiful.butterfly.schedule.TaskItemAndParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;


public class DBDemoSingleTaskDeal implements SingleTaskDeal<Long> {

    private static transient Logger log = LoggerFactory.getLogger(DBDemoSingleTaskDeal.class);

    protected DataSource dataSource;

    public Comparator<Long> getComparator() {
        return new Comparator<Long>() {
            public int compare(Long o1, Long o2) {
                return o1.compareTo(o2);
            }

            public boolean equals(Object obj) {
                return this == obj;
            }
        };
    }

    public List<Long> getTaskList(String taskParameter, String ownSign, int taskItemNum,
                                  int fetchNum, List<TaskItemAndParameter> taskItemAndParameterList) throws Exception {
        List<Long> longs = new ArrayList<Long>();
        if (taskItemAndParameterList.size() == 0) {
            return longs;
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < taskItemAndParameterList.size(); i++) {
            if (i > 0) {
                sb.append(",");
            }
            sb.append(taskItemAndParameterList.get(i).getTaskItem());
        }

        Connection connection = null;
        try {
            connection = dataSource.getConnection();

            //and mod(id," + taskItemNum + ") in (" + sb.toString() + ") and sts ='n' LIMIT " + fetchNum;
            String sql = "select ID from SCHEDULE_TEST where OWN_SIGN = '" + ownSign + "'  and mod(id," + taskItemNum + ") in (" + sb.toString() + ") and sts ='n' LIMIT " + fetchNum;
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                longs.add(resultSet.getLong("id"));
            }
            resultSet.close();
            preparedStatement.close();
            return longs;
        } finally {
            if (connection != null) {
                connection.close();
            }

        }
    }


    public boolean execute(Long task, String ownSign) throws Exception {
        Connection connection = null;
        Long id = task;
        try {
            connection = dataSource.getConnection();
            String sql = "update schedule_test set sts ='y' ,deal_count = deal_count + 1 where id = ? and sts ='n' ";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setLong(1, id);
            preparedStatement.executeUpdate();
            preparedStatement.close();
            connection.commit();

            return true;
        } catch (Exception e) {

            if (connection != null) {
                connection.rollback();
            }
            return false;
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }


}